package org.lease.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.lease.common.result.Result;
import org.lease.model.dto.RoomItemDTO;
import org.lease.model.dto.RoomSaveDTO;
import org.lease.model.entity.RoomInfo;
import org.lease.model.enums.ReleaseStatus;
import org.lease.model.query.RoomPageQuery;
import org.lease.model.vo.RoomDetailVo;
import org.lease.service.RoomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
public class RoomController {
    @Autowired
    private RoomInfoService roomInfoService;

    @Operation(summary = "分页条件查询房间列表")
    @GetMapping("/pageItem")
    public Result<IPage<RoomItemDTO>> pageItem(RoomPageQuery query) {
        IPage<RoomItemDTO> page = new Page<>(query.getCurrent(), query.getSize());
        IPage<RoomItemDTO> result = roomInfoService.pageQueryRoomItem(page, query);

        return Result.success(result);
    }

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSaveDTO roomSaveDTO) {
        roomInfoService.saveOrUpdateRoom(roomSaveDTO);
        return Result.success();
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("/getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomInfo = roomInfoService.getRoomDetailById(id);
        return Result.success(roomInfo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id) {
        roomInfoService.removeRoomById(id);
        return Result.success();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("/updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<RoomInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoomInfo::getId, id);
        updateWrapper.set(RoomInfo::getIsRelease, status);
        roomInfoService.update(updateWrapper);

        return Result.success();
    }

    @Operation(summary = "根据公寓id查询房间列表")
    @GetMapping("/listBasicByApartmentId")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        queryWrapper.eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED);
        List<RoomInfo> roomInfoList = roomInfoService.list(queryWrapper);

        return Result.success(roomInfoList);
    }
}
